﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;

namespace KorbeRTR
{
    public partial class FormTemperatureCurve : Form
    {
        /// <summary>
        /// 曲线名称列表
        /// </summary>
        private string[] CurveNames;

        /// <summary>
        /// 曲线颜色列表
        /// </summary>
        private Color[] CurveColors;

        /// <summary>
        /// 温度记录
        /// </summary>
        private List<TemperatureRecord> TempList;

        public FormTemperatureCurve()
        {
            InitializeComponent();

       
        
                CurveNames = new string[]
                {
                    "全部",
                    "除油",
                    "热水洗",
                    "整流机冷却",
                    "酸洗槽",
                    "镀铜",
                    "微蚀",
                    "铜槽添加",
                    "烘干加热",
                    "酸浸",
                    "抗氧化",
                    "热水槽"
                };
            

            CurveColors = new Color[]
            {
                Color.DodgerBlue,
                Color.FromArgb(99,178,238),
                Color.FromArgb(118,218,145),
                Color.FromArgb(248,203,127),
                Color.FromArgb(248,149,136),
                Color.FromArgb(124,214,207),
                Color.FromArgb(145,146,171),
                Color.FromArgb(120,152,225),
                Color.FromArgb(239,166,102),
                Color.FromArgb(237,221,134),
                Color.FromArgb(153,135,206),
            };

            TempList = new List<TemperatureRecord>();
        }

        private void FormTemperatureCure_Load(object sender, EventArgs e)
        {
            ComboBox_Slot.DataSource = CurveNames;

            DTP_End.Value = DateTime.Now.Date;
            DTP_Start.Value = DTP_End.Value.AddDays(-2);

            TempList = TemperatureRecord.Select(DTP_Start.Value, DTP_End.Value.AddDays(1));
            UpdateCurve(TempList, ComboBox_Slot.SelectedIndex);
        }

        private void UpdateCurve(List<TemperatureRecord> list, int index)
        {
            HCH_Temperature.RemoveAllCurve();

            bool isB = ConfigurationManager.AppSettings["LINE"] == "B";

            var array = (from iter in list orderby iter.Time ascending select iter).ToArray();
            var times = new DateTime[array.Length];
            var temp = new float[CurveNames.Length][];
            for(int i=0; i<temp.Length; i++)
            {
                temp[i] = new float[array.Length];
            }
            for (int i = 0; i < array.Length; i++)
            {
                times[i] = array[i].Time;

                int row = 1;
                temp[row++][i] = (float)array[i].除油温度;
                temp[row++][i] = (float)array[i].热水洗温度;
                temp[row++][i] = (float)array[i].整流机冷却;
                temp[row++][i] = (float)array[i].酸洗槽;
                temp[row++][i] = (float)array[i].镀铜;
                temp[row++][i] = (float)array[i].微蚀;
                temp[row++][i] = (float)array[i].热水槽;
                temp[row++][i] = (float)array[i].烘干加热;
                temp[row++][i] = (float)array[i].酸浸;
                temp[row++][i] = (float)array[i].抗氧化;
            
            }

            if (index != 0)
            {
                HCH_Temperature.SetDateTimes(times);
                HCH_Temperature.SetLeftCurve(CurveNames[index], temp[index], CurveColors[0], HslControls.CurveStyle.LineSegment, "{0:F1} ℃");
            }
            else
            {   // 全部显示
                HCH_Temperature.SetDateTimes(times);

                for (int i=1; i< CurveNames.Length; i++)
                {
                    HCH_Temperature.SetLeftCurve(CurveNames[i], temp[i], CurveColors[i], HslControls.CurveStyle.LineSegment, "{0:F1} ℃");
                }
            }

            HCH_Temperature.RenderCurveUI();
        }

        private void ComboBox_Slot_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ComboBox_Slot.SelectedIndex < 0) return;

            UpdateCurve(TempList, ComboBox_Slot.SelectedIndex);
        }

        private void DTP_Start_ValueChanged(object sender, EventArgs e)
        {
            TempList = TemperatureRecord.Select(DTP_Start.Value, DTP_End.Value.AddDays(1));
            UpdateCurve(TempList, ComboBox_Slot.SelectedIndex);
        }

        private void DTP_End_ValueChanged(object sender, EventArgs e)
        {
            TempList = TemperatureRecord.Select(DTP_Start.Value, DTP_End.Value.AddDays(1));
            UpdateCurve(TempList, ComboBox_Slot.SelectedIndex);
        }
    }
}
